<?php
namespace app\index\controller;
use think\Controller;
use think\Db;

class Other extends Controller
{
    public $field_inner = "d.driver_id,driver_name,car_plate,car_model,car_brand,color";
    public $field_inter = "d.id as driver_id,driver_name,car_plate,car_model,car_brand,car_color as color";
	//
	public function about()
	{ 
		$this->redirect('api/other/about');
	}
	//*****计价规则
	public function computer_price(){
		$adcode = input('get.adCode');
		//$order_type = input('get.order_type');
		if($adcode){
			$data = Db::name('region r')->join('zone z','r.region_id=z.region_id')->where('r.region_code',$adcode)->find();
			if(!$data){
				//如果是市辖区的adcode，直接显示为该城市的region_id
				$data = Db::name('region r')->join('zone z','r.parent_id=z.region_id')->where('r.region_code',$adcode)->find();
			}
			if(!$data){
				$region_id = $data ? $data['region_id'] : 133 ;//如果APP端没有获取adcode 就默认合肥
				$data = Db::name('zone')->where('region_id',$region_id)->find();
			}
		}
		if(input('get.region_id')){
			$data = Db::name('zone')->where('region_id',input('get.region_id'))->find();
		}
		$data['mile_price'] = json_decode($data['price'],true)['mile'][0]['value'];
		$list = Db::name('zone')->where('is_use',1)->order('region_id asc')->select();
		$this->assign('data',$data);
		$this->assign('list',$list);
		return $this->fetch();

	}

	public function showAllWorkDriverLocation()
    {
        $config['protocol'] = PROTOCOL;
        $config['gd_key'] = config('GD_WEB');
        $config['url_inter'] = url('/admin/inter_driver/local');
        $config['url_inner'] = url('/admin/driver/local');
        $this->assign($config);
        return view('local');
    }

    public function getLocal()
    {
        $driver_combine = array();
        $post = input('post.');
        $mongo = db('driver_location',config('mongodb'));
        switch ($post['type']){
            case 'init':
                $code = json_decode($post['code'],true);
                $where_inner['region_code'] = $where_inter['region_code'] = $code['value'];
                $where_inner['record_status'] = $where_inter['forbid'] = 2;
                switch ($code['key']){
                    case 'city':
                        $inner_driver = $this->searchInnerDriver($where_inner,$this->field_inner);
                        $inner_driver = decode_array_2($inner_driver);
                        $inner_driver = $this->dealDriverInfo($inner_driver);
                        $map['driver_id'] = array('in',$inner_driver['driver_id']);
                        $map['type'] = array('in',array('出租车','城内专车'));
                        $inter_driver = $this->searchInterDriver($where_inter,$this->field_inter);
                        $inter_driver = $this->dealDriverInfo($inter_driver);
                        $map_['driver_id'] = array('in',$inter_driver['driver_id']);
                        $map_['type'] = '城际拼车';
                        break;
                    default:
                        $inner_driver = $this->searchInnerDriver($where_inner,$this->field_inner,false);
                        $inner_driver = decode_array_2($inner_driver);
                        $inner_driver = $this->dealDriverInfo($inner_driver);
                        $map['driver_id'] = array('in',$inner_driver['driver_id']);
                        $map['type'] = array('in',array('出租车','城内专车'));
                        $inter_driver = $this->searchInterDriver($where_inter,$this->field_inter,false);
                        $map_['driver_id'] = array('in',$inter_driver['driver_id']);
                        $map_['type'] = '城际拼车';
                        break;
                }
                $inner_local = $mongo->where($map)->select();$inner_local = $this->dealDriverInfoLocation($inner_driver,$inner_local);
                $inter_local = $mongo->where($map_)->select();$inter_local = $this->dealDriverInfoLocation($inter_driver,$inter_local);
                $inner_local && $driver_combine = array_merge($driver_combine,$inner_local);
                $inter_local && $driver_combine = array_merge($driver_combine,$inter_local);
                break;
            default:
                $map['location'] = array('geo within',array('$box'=>json_decode($post['bounds'],true)));
                $driver_combine = $mongo->where($map)->select();
                $driver_combine = $this->dealDriverLocation($driver_combine);
                break;
        }
        /********************************************************城内*****************************************************************************/
        /*$map_inner['d.driver_status'] = array('in',array(2,3,4));
        $map_inner['d.auth_status'] = 1;
        $map_inner['ci.record_status'] = 2;
        $map_inner['oc.order_status'] = array('in',array('20','30','40'));
        $driver_inner = Db::name('order_common oc')->join('driver d','oc.driver_id=d.driver_id','LEFT')
            ->join('car_info ci','d.driver_id=ci.driver_id','LEFT')->where($map_inner)
            ->field('common_id,order_type,order_sn,oc.driver_id,d.driver_name,local_table,ci.car_plate')->select();
        foreach ($driver_inner as $key => $val){
            $driver_inner[$key]['car_plate'] = Security::decrypt($val['car_plate']);
            $driver_inner[$key]['type'] = order_type()[$val['order_type']];
            unset($driver_inner[$key]['common_id']);
            unset($driver_inner[$key]['driver_id']);
            unset($driver_inner[$key]['local_table']);
            $map_local['order_id'] = $val['common_id'];
            $map_local['driver_id'] = $val['driver_id'];
            $local = Db::name($val['local_table'])->where($map_local)->order('id desc')->field('direction,longitude,latitude')->find();
            if($local) {$driver_inner[$key]['location'] = array($local['longitude'],$local['latitude']);$driver_inner[$key]['direction'] = $local['direction'];}
            unset($local);
        }
        $driver_inner&&$driver_combine = array_merge($driver_combine,$driver_inner);*/
        /********************************************************城际*****************************************************************************/
        /*$intercity_db = config('intercity_db');
        $map_inter['d.is_goon'] = array('in',array(1,3));
        $map_inter['d.auth_status'] = 1;
        $driver_inter = Db::connect($intercity_db)->name('driver d')->join('car_list cl','d.id=cl.driver','LEFT')->where($map_inter)
            ->field('d.id as driver_id,d.driver_name,car_plate,direction,car_lng as longitude,car_lat as latitude')->select();
        foreach ($driver_inter as $key => $val){
            $map_inter_o['driver_id'] = $val['driver_id'];
            $map_inter_o['order_status'] = array('in',array(35,40));
            $order_sn = Db::table('inter_order_common')->where($map_inter_o)->column('order_sn');
            $order_sn && $driver_inter[$key]['order_sn'] = implode('；',$order_sn);
            $driver_inter[$key]['location'] = array($val['longitude'],$val['latitude']);
            $driver_inter[$key]['type'] = '城际拼车';
            unset($driver_inter[$key]['longitude']);
            unset($driver_inter[$key]['latitude']);
            unset($driver_inter[$key]['driver_id']);
            unset($order);
        }
        $driver_inter&&$driver_combine = array_merge($driver_combine,$driver_inter);*/
        if(empty($driver_combine)){
            return json(array('status'=>0,'msg'=>'当前条件暂无车辆'));
        }
        return json(array('status'=>1,'data'=>$driver_combine));
    }

    /**
     * 处理查询出来的司机信息
     * @param $driver_info //司机信息
     * @return array
     * */
    private function dealDriverInfo($driver_info)
    {
        if(empty($driver_info)){
            return array('driver_id'=>array());
        }
        foreach ($driver_info as $key => $val){
            $driver_info['driver_id'][] = $val['driver_id'];
            unset($driver_info[$key]);
            $driver_info[$val['driver_id']] = $val;
        }
        return $driver_info;
    }

    /**
     * 融合司机信息和司机坐标信息
     * @param $driver_info //司机信息
     * @param $local //司机坐标信息
     * @return  boolean|array
     * */
    private function dealDriverInfoLocation($driver_info,$local)
    {
        if(empty($local) || empty($driver_info)){
            return false;
        }
        foreach ($local as $key => $val){
            $driver_info[$val['driver_id']]&&$local[$key] = array_merge($val,$driver_info[$val['driver_id']]);
        }
        return $local;
    }

    /**
     * 处理司机坐标信息
     * @param $driver_info //司机坐标信息
     * @return array|boolean //司机坐标信息
     * */
    private function dealDriverLocation($local)
    {
        if(empty($local)) return false;
        $inter_driver_local = $inner_driver_local = $where_inner = $where_inter = $data = array();
        foreach ($local as $key => $val){
            switch ($val['type']){
                case '城际拼车':
                    $inter_driver_local['driver_id'][]=$val['driver_id'];
                    $inter_driver_local[$key] = $val;
                    break;
                default:
                    $inner_driver_local['driver_id'][]=$val['driver_id'];
                    $inner_driver_local[$key] = $val;
                    break;
            }
        }
        /************************************************城内************************************************/
        if($inner_driver_local){
            $where_inner['record_status'] = 2;
            $where_inner['d.driver_id'] = array('in',$inner_driver_local['driver_id']);
            unset($inner_driver_local['driver_id']);
            $inner_driver = $this->searchInnerDriver($where_inner,$this->field_inner);
            if($inner_driver){
                $inner_driver = $this->dealDriverInfo($inner_driver);unset($inner_driver['driver_id']);
                $inner_driver = $this->dealDriverInfoLocation($inner_driver,$inner_driver_local);
                $data = array_merge($data,$inner_driver); $data = decode_array_2($data);
            }
        }
        /************************************************城际************************************************/
        if($inter_driver_local){
            $where_inter['d.id'] = array('in',$inter_driver_local['driver_id']);
            unset($inter_driver_local['driver_id']);
            $inter_driver = $this->searchInterDriver($where_inter,$this->field_inter);
            if($inter_driver){
                $inter_driver = $this->dealDriverInfo($inter_driver);unset($inter_driver['driver_id']);
                $inter_driver = $this->dealDriverInfoLocation($inter_driver,$inter_driver_local);
                $data = array_merge($data,$inter_driver);
            }
        }
        return $data;
    }

    /**
     * 搜索城内司机
     * @param $map
     * @param $field
     * @param $flag //当map含有region_code时，true为城市，false为县区
     * @return  mixed
     * */
    private function searchInnerDriver($map,$field='*',$flag=true)
    {
        return $flag ? Db::name('driver d')->join('region r','d.work_city=r.region_id','LEFT')
            ->join('car_info ci','ci.driver_id=d.driver_id','LEFT')->where($map)
            ->field($field)->select():
            Db::name('driver d')->join('region r','d.work_city=r.parent_id','LEFT')
                ->join('car_info ci','ci.driver_id=d.driver_id','LEFT')->where($map)
                ->field($field)->select();
    }

    /**
     * 搜索城际司机
     * @param $map
     * @param $field
     * @param $flag //当map含有region_code时，true为城市，false为县区
     * @return  mixed
     * */
    private function searchInterDriver($map,$field='*',$flag=true)
    {
        return $flag ? Db::table('inter_driver d')->join('region r','d.work_city=r.region_id','LEFT')
            ->join(config('intercity_db')['database'].'.inter_car_list cl','cl.driver=d.id','LEFT')
            ->where($map)->field($field)->select():
            Db::table('inter_driver d')->join('region r','d.work_city=r.parent_id','LEFT')
            ->join(config('intercity_db')['database'].'.inter_car_list cl','cl.driver=d.id','LEFT')
            ->where($map)->field($field)->select();
    }
}
